const ExcelJS = require('exceljs');


// 获取当前时间
var getNowTime = function () {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        return year + "_" + month+ "_" + day + "_" + hour + "_" + minute + "_" + second
    }

/* 根据六大属性计算总评 */
const calculateOverall = function(player){
    let overallRating = 0;

    // 根据不同位置赋予技能权重
    let weights;
    switch (player.playerPosition) {
        case "前锋":
            weights = {
                playerSho: 0.3,
                playerPac: 0.25,
                playerDri: 0.2,
                playerPhy: 0.15,
                playerPas: 0.1,
            };
            break;
        case "中场":
            weights = {
                playerPas: 0.3, /* 传球 */
                playerDri: 0.25, /* 盘带 */
                playerPhy: 0.15, /* 身体 */
                playerPac: 0.1, /* 速度 */
                playerSho: 0.1, /* 射门 */
                playerDef: 0.1 /* 防守 */
            };
            break;
        case "后卫":
            weights = {
                playerDef: 0.3,
                playerPhy: 0.25,
                playerPac: 0.2,
                playerDri: 0.1,
                playerPas: 0.1,
                playerSho: 0.05
            };
            break;
        case "门将":
            weights = {
                playerDef: 0.35,
                playerPhy: 0.25,
                playerPac: 0.2,
                playerPas: 0.15,
                playerDri: 0.05
            };
            break;
        default:
            weights = {
                playerSho: 0.17,
                playerPac: 0.17,
                playerDri: 0.17,
                playerPhy: 0.17,
                playerPas: 0.17,
                playerDef: 0.15
            };
            break;
    }

    // 计算总评分
    for (let skill in weights) {
        overallRating += player[skill] * weights[skill];
    }

    return overallRating.toFixed(0);
}

/* 格式化 对象键名 */
function toCamelCase(str) {
    return str.replace(/_([a-z])/g, function(match, letter) {
        return letter.toUpperCase();
    });
}

function formatObjectKeys(obj) {
    const formattedObj = {};
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            const formattedKey = toCamelCase(key);
            if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
                formattedObj[formattedKey] = formatObjectKeys(obj[key]);
            } else {
                formattedObj[formattedKey] = obj[key];
            }
        }
    }
    return formattedObj;
}


async function exportToExcel(data, filePath) {
    // 创建一个工作簿
    const workbook = new ExcelJS.Workbook();

    // 添加一个工作表
    const worksheet = workbook.addWorksheet('Sheet1');

    // 添加表头
    worksheet.addRow(['Name', 'Age', 'Country']);

    // 添加数据行
    data.forEach(row => {
        worksheet.addRow(row);
    });

    try {
        // 生成 Excel 文件
        await workbook.xlsx.writeFile(filePath);
        console.log('Excel 文件已生成:', filePath);
    } catch (error) {
        console.error('生成 Excel 文件时出错:', error);
    }
}

/* // 使用示例
const data = [
    ['John', 30, 'USA'],
    ['Alice', 25, 'UK'],
    ['Bob', 35, 'Canada']
];

const filePath = 'output.xlsx';
exportToExcel(data, filePath); */


module.exports = {
  getNowTime,
  calculateOverall,
  formatObjectKeys,
  exportToExcel
}